<?php

/**
 * manufacturers actions.
 *
 * @package    sf_sandbox
 * @subpackage manufacturers
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class manufacturersActions extends sfActions
{
  public function executeIndex()
  {
    $this->manufacturersList = ManufacturersPeer::doSelect(new Criteria());
  }

  public function executeCreate()
  {
    $this->form = new ManufacturersForm();

    $this->setTemplate('edit');
  }

  public function executeEdit($request)
  {
    $this->form = new ManufacturersForm(ManufacturersPeer::retrieveByPk($request->getParameter('manufacturers_id')));
  }

  public function executeUpdate($request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new ManufacturersForm(ManufacturersPeer::retrieveByPk($request->getParameter('manufacturers_id')));

    $this->form->bind($request->getParameter('manufacturers'),$request->getFiles('manufacturers'));
    
    if ($this->form->isValid())
    {
      $manufacturers = $this->form->getObject();
      $manufacturers->save();
      
      $file = $this->form->getValue('manufacturers_image');
      //$filename = sha1($file->getOriginalName()).$file->getExtension($file->getOriginalExtension());
      $filename = $manufacturers->getManufacturersId().$file->getExtension($file->getOriginalExtension());
      $file->save(sfConfig::get('sf_upload_dir').'/manufacturers/originals/'.$filename);
      $manufacturers->setManufacturersImage($filename);
      $manufacturers->save();

      $thumbnail = new sfThumbnail(150, 150);
      $thumbnail->loadFile(sfConfig::get('sf_upload_dir').'/manufacturers/originals/'.$filename);
      $thumbnail->save(sfConfig::get('sf_upload_dir').'/manufacturers/thumbnails/'.$filename);
      
      /*$thumbnail = new sfThumbnail(50, 50);
      $thumbnail->loadFile(sfConfig::get('sf_upload_dir').'/manufacturers/originals/'.$filename);
      $thumbnail->save(sfConfig::get('sf_upload_dir').'/manufacturers/cart_thumbnails/'.$filename);
      */
      $this->redirect('manufacturers/edit?manufacturers_id='.$manufacturers->getManufacturersId());
    }

    $this->setTemplate('edit');
  }

  public function executeDelete($request)
  {
    $this->forward404Unless($manufacturers = ManufacturersPeer::retrieveByPk($request->getParameter('manufacturers_id')));

    $manufacturers->delete();

    $this->redirect('manufacturers/index');
  }
}
